
define(['jquery','bootstrap', window._sys_config.require_config.baseUrl+'js/common.js'],function($){
	$(function() {
		function dataPic600() {
			var dataJson = {'data': [
				{
					'src': 'https://img.alicdn.com/imgextra/i1/1819472038/TB2GPAxg8NkpuFjy0FaXXbRCVXa_!!1819472038.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.26.QOyerh&id=528912062232&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/23501375/TB2af9SaRYxQeBjSspdXXb6QXXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i3/23501375/TB2ON9TaKTyQeBjSspfXXaI3FXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i3/23501375/TB2oz1hk5pnpuFjSZFkXXc4ZpXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i3/23501375/TB2DuSSaRYxQeBjSspdXXb6QXXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i1/23501375/TB2Ms11aNfxQeBjSsppXXXeoFXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/23501375/TB2NxCTaKTyQeBjSspfXXaI3FXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/23501375/TB2kSzgaBLxQeBjy0FnXXcQwpXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i1/23501375/TB2Ms11aNfxQeBjSsppXXXeoFXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i1/23501375/TB2PmGTa8TxQeBjSspoXXcP7XXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i1/23501375/TB2EJyOaWzyQeBjy0FbXXbZapXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i2/23501375/TB2erRtXV_AQeBjSZPhXXXt5pXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i1/23501375/TB2x715a3_xQeBjy0FoXXX1vpXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/23501375/TB2RcSQaWzyQeBjy0FgXXbnsVXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i3/23501375/TB2NaSOa8TxQeBjSspkXXXtMpXa_!!23501375.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.1.zvpGJ0&id=544509918876&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/1757906504/TB2HwwUjbJmpuFjSZFwXXaE4VXa_!!1757906504.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.63.sSgTNM&id=547015328219&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/1757906504/TB23DdljypnpuFjSZFkXXc4ZpXa_!!1757906504.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.63.sSgTNM&id=547015328219&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/1757906504/TB2ObM5hStkpuFjy0FhXXXQzFXa_!!1757906504.jpg',
					'href': 'https://item.taobao.com/item.htm?spm=a230r.1.999.63.sSgTNM&id=547015328219&ns=1#detail'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i3/50454948/TB2Sjofdl8kpuFjSspeXXc7IpXa_!!50454948.jpg',
					'href': 'https://item.taobao.com/item.htm?id=544675714813'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i3/50454948/TB28_IudYJmpuFjSZFwXXaE4VXa_!!50454948.jpg',
					'href': 'https://item.taobao.com/item.htm?id=544675714813'
				},
				{
					'src': 'https://img.alicdn.com/imgextra/i4/50454948/TB2LYMsd4tmpuFjSZFqXXbHFpXa_!!50454948.jpg',
					'href': 'https://item.taobao.com/item.htm?id=544675714813'
				}
			]}
			$.each(dataJson.data,function(index,dom){
				var $pin = $('<div class="pin wfc wft"></div>');
			    var $a = $('<a class="img"></a>');
			    $a.attr('href', $(dom).attr('href'));
			    var $div = $('<div class="ani-wrapper"></div>');
			    var $img = $("<img />");
			    $img.attr({src:$(dom).attr('src') + '_600x600'});
			    $div.append($img);
			    $pin.append($a);
			    $a.append($div);
			    $('#waterfall').append($pin);
				
			});
		}
		dataPic600();
		$('.tishi').height($(window).height() - 262)
		var t_img; // 定时器
		var isLoad = true; // 控制变量
		
		// 判断图片加载状况，加载完成后回调
		isImgLoad(function(){
			waterFall('waterfall','pin');
			$('.tishi').hide();
		    // 加载完成
		});
		
		// 判断图片加载的函数
		function isImgLoad(callback){
		    // 注意我的图片类名都是cover，因为我只需要处理cover。其它图片可以不管。
		    // 查找所有封面图，迭代处理
		    $('.pin img').each(function(){
		        // 找到为0就将isLoad设为false，并退出each
		        if(this.height === 0){
		            isLoad = false;
		            return false;
		        }
		    });
		    // 为true，没有发现为0的。加载完毕
		    if(isLoad){
		        clearTimeout(t_img); // 清除定时器
		        // 回调函数
		        callback();
		    // 为false，因为找到了没有加载完成的图，将调用定时器递归
		    }else{
		        isLoad = true;
		        t_img = setTimeout(function(){
		            isImgLoad(callback); // 递归扫描
		        },5); // 我这里设置的是500毫秒就扫描一次，可以自己调整
		    }
		}
		/* 
		* parend 父级id
		* clsName 元素class
		* */
		function waterFall(parent, clsName){
			var firstLeft = 0, firstTop = 0;
			var $parent = $('#'+parent);//父元素
			var $boxs = $parent.find('.'+clsName);//所有box元素
			var iPinW = $boxs.eq( 0 ).width()+10;// 一个块框box的宽
			var cols = Math.ceil( $parent.width() / iPinW );//列数
			var pinHArr=[];//用于存储 每列中的所有块框相加的高度。
			$boxs.each( function( index, dom){
				if( index < cols ){
					$boxs.eq(index).css({'position': 'absolute', 'top': '0px', 'left': firstLeft});
					firstLeft = firstLeft + iPinW;
					pinHArr[ index ] = $(dom).height(); //所有列的高度
				}else{
					var minH = Math.min.apply( null, pinHArr );//数组pinHArr中的最小值minH
					var minHIndex = $.inArray( minH, pinHArr );
					$(dom).css({
						'position': 'absolute',
						'top': minH + 15,
				        'left': $boxs.eq( minHIndex ).position().left
				    });
				    //添加元素后修改pinHArr
				    pinHArr[ minHIndex ] += $(dom).height() + 15;//更新添加了块框后的列高
				}
			});
			var $lastBox = $('#waterfall').find('.pin').last(),
				lastBoxH = $lastBox.offset().top + $lastBox.height();
			$('#waterfall').height(lastBoxH);
		};
		//检验是否满足加载数据条件，即触发添加块框函数waterfall()的高度：最后一个块框的距离网页顶部+自身高的一半(实现未滚到底就开始加载)
		function checkscrollside(parent,clsName){
		//最后一个块框
			var $lastBox = $('#'+parent).find('.'+clsName).last(),
				lastBoxH = $lastBox.offset().top + $lastBox.height(),
				windowH = $(window).height(),
				scrollTop = $(window).scrollTop(),
				documentH = $(document).height();
				return scrollTop + windowH > documentH-200 ? true : false;
		}
		$(window).on('resize',function(){
			waterFall('waterfall', 'pin');
		})
		$(window).scroll(function() {
			//	模拟数据json
			var dataJson = {'data':
				[
					{
				    	'src':'https://img.alicdn.com/imgextra/i4/825890122/TB2.RJseUhnpuFjSZFEXXX0PFXa_!!825890122.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=544675714813'
				    },
				    {
				    	'src':'https://img.alicdn.com/imgextra/i2/825890122/TB23PtieSVmpuFjSZFFXXcZApXa_!!825890122.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=544675714813'
				    },	
				    {
				    	'src':'https://img.alicdn.com/imgextra/i2/71460541/TB22N2UdrFkpuFjy1XcXXclapXa_!!71460541.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=544531125693'
				    },
				    {
				    	'src':'https://img.alicdn.com/imgextra/i3/71460541/TB2nXJceC8mpuFjSZFMXXaxpVXa_!!71460541.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=544531125693'
				    },
				    {
				    	'src':'https://img.alicdn.com/imgextra/i4/380101244/TB2a1zJfC0mpuFjSZPiXXbssVXa_!!380101244.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=545305231081'
				    },
				    {
				    	'src':'https://img.alicdn.com/imgextra/i1/380101244/TB2iFbReCJjpuFjy0FdXXXmoFXa_!!380101244.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=545305231081'
				    },
				    {
				    	'src':'https://img.alicdn.com/imgextra/i3/380101244/TB24EjFfxxmpuFjSZFNXXXrRXXa_!!380101244.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=545305231081'
				    },
				    {
				    	'src': 'https://img.alicdn.com/imgextra/i4/21659330/TB2d6ipc0hvOuFjSZFBXXcZgFXa_!!21659330.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=546028820750'
				    },
				    {
				    	'src': 'https://img.alicdn.com/imgextra/i3/21659330/TB231yvc4RDOuFjSZFzXXcIipXa_!!21659330.jpg',
				    	'href': 'https://item.taobao.com/item.htm?id=546028820750'
				    }
				]
			};
			var isPosting = false;
			if(checkscrollside('waterfall','pin') && !isPosting){
			    isPosting = true;
			    $.each(dataJson.data,function(index,dom){
			    	var $pin = $('<div class="pin wfc wft"></div>');
			    	var $a = $('<a class="img"></a>');
			    	$a.attr('href', $(dom).attr('href'));
			    	var $div = $('<div class="ani-wrapper"></div>');
			    	var $img = $("<img />");
			    	$img.attr({src:$(dom).attr('src') + '_600x600'});
			    	$div.append($img);
			        $pin.append($a);
			        $a.append($div);
			        $('#waterfall').append($pin);
			        isPosting = false;
			    });
			    setTimeout(function() {
			    	waterFall('waterfall','pin');
			    }, 30) 
			}
		})
		//通过委托的方式代理
		$("#waterfall").on("mouseover", ".pin", function() {
			$('.pop-img-box').find('img').remove();
			var leftPosition = $(this).position().left;
			var offset = $(this).offset().top;
			var divHeight = $(this).height()*1.5;
			var windowInnerHeight = $(window).height();
			if((offset + divHeight > windowInnerHeight + $(window).scrollTop()) && ($('#waterfall').width() - leftPosition < 235*1.5)) {
				$(this).css({'transform-origin': '100% 100%'})
			}else if(offset + divHeight > windowInnerHeight + $(window).scrollTop()) {
				$(this).css({'transform-origin': '0% 100%'})
			}else if($('#waterfall').width() - leftPosition < 235*1.5) {
				$(this).css({'transform-origin': '100% 0%'})
			}else {
				$(this).css({'transform-origin': '0% 0%'})
			}
			$(this).css({'transform': 'scale(1.5)', 'z-index': '200'})
		})
		$("#waterfall").on("mouseout", ".pin", function() {
			$(this).css({'transform': 'none', 'z-index': '10'})
		})
	})
});
